//
//  CollectionViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/6.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "CollectionViewController.h"
#import "FindCollectionViewCell.h"
#import "MusicListViewController.h"
#import "XMCategory.h"
#import "XMReqMgr.h"
#import "XMAlbum.h"
#import "MJRefresh.h"

@interface CollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,assign)int page;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)refreshBaseControlValue
{
    [self updateCategoryInfoWithPage:1];
}

#pragma mark --- 事件处理
- (void)updateCategoryInfoWithPage:(int)page
{
    NSString *categoryID = [NSString stringWithFormat:@"%li",(long)self.categoryModel.categoryId];
    NSDictionary *params = @{@"category_id":categoryID,@"calc_dimension":@"1",@"page":@(page).stringValue};
    [[XMReqMgr sharedInstance] requestXMData:XMReqType_MetadataAlbums params:params withCompletionHander:^(id result, XMErrorModel *error) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (!error)
        {
            NSMutableArray *array = [NSMutableArray arrayWithCapacity:4];
            for (NSDictionary *dict in result[@"albums"])
            {
                XMAlbum *model = [[XMAlbum alloc] initWithDictionary:dict];
                //                NSLog(@"%@",model.albumTitle);
                [array addObject:model];
            }
            if (array.count == 0)
            {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            else
            {
                if (self.page == 1)
                {
                    [self.dataSource removeAllObjects];
                    self.dataSource = array;
                }
                else
                {
                    [self.dataSource addObjectsFromArray:array];
                }
                [self.collectionView reloadData];
            }

        }
        else
        {
            
        }
    }];

}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindCollectionViewCell *cell = [FindCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    XMAlbum *model = self.dataSource[indexPath.row];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.coverUrlLarge] placeholderImage:imageWithName(@"pic")];
    cell.titleLabel.text = model.albumTitle;
    cell.nameLabel.text = model.albumIntro;
//    cell.backgroundColor = indexPath.row %2 ? [UIColor redColor] : [UIColor greenColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbum *model = self.dataSource[indexPath.row];
    MusicListViewController *vc = [MusicListViewController new];
    vc.albumModel = model;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --- 懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(px1080Width(472) ,px1920Height(480));
        layout.minimumLineSpacing = px1920Height(40);
        layout.minimumInteritemSpacing = px1920Height(20);
        layout.sectionInset = UIEdgeInsetsMake(px1080Width(40), px1080Width(40), px1080Width(40), px1080Width(40));
        _collectionView = ({
            UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
            collectionView.backgroundColor = rgba(6,27,56,1);
            collectionView.dataSource = self;
            collectionView.delegate = self;
            [self.view addSubview:collectionView];
            collectionView;
        });
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self updateCategoryInfoWithPage:self.page];
        }];
        
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            [self updateCategoryInfoWithPage:self.page];
        }];
        
        
    }return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(20));
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(-49);
        make.top.left.right.mas_equalTo(superView);
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavigationStyle
{
    [self setLeftButtonImage:nil];
}

@end
